from CoolProp.CoolProp import PropsSI as props
from scipy import constants as const

# ASHRAE的数据：制冷量的单位为kcal/h
data1 = {
    # 第一张表格中的数据
    (40, -40): {"制冷量": 201, "功率": 199, "电流": 1.13},
    (40, -35): {"制冷量": 265, "功率": 224, "电流": 1.23},
    (40, -30): {"制冷量": 345, "功率": 248, "电流": 1.33},
    (40, -25): {"制冷量": 441, "功率": 273, "电流": 1.44},
    (40, -23.3): {"制冷量": 477, "功率": 282, "电流": 1.47},
    (40, -20): {"制冷量": 552, "功率": 298, "电流": 1.54},
    (40, -15): {"制冷量": 679, "功率": 323, "电流": 1.65},
    (40, -10): {"制冷量": 822, "功率": 348, "电流": 1.76},
    (40, -5): {"制冷量": 980, "功率": 374, "电流": 1.87},
    (40, 0): {"制冷量": 1155, "功率": 400, "电流": 1.99},
    # 第二张表格中的数据
    (45, -40): {"制冷量": 188, "功率": 200, "电流": 1.13},
    (45, -35): {"制冷量": 251, "功率": 227, "电流": 1.24},
    (45, -30): {"制冷量": 329, "功率": 255, "电流": 1.36},
    (45, -25): {"制冷量": 423, "功率": 283, "电流": 1.48},
    (45, -23.3): {"制冷量": 458, "功率": 292, "电流": 1.52},
    (45, -20): {"制冷量": 532, "功率": 311, "电流": 1.60},
    (45, -15): {"制冷量": 657, "功率": 339, "电流": 1.72},
    (45, -10): {"制冷量": 798, "功率": 367, "电流": 1.84},
    (45, -5): {"制冷量": 955, "功率": 395, "电流": 1.97},
    (45, 0): {"制冷量": 1127, "功率": 424, "电流": 2.10},
    # 第三张表格中的数据
    (50, -40): {"制冷量": 175, "功率": 201, "电流": 1.14},
    (50, -35): {"制冷量": 236, "功率": 231, "电流": 1.26},
    (50, -30): {"制冷量": 312, "功率": 262, "电流": 1.39},
    (50, -25): {"制冷量": 404, "功率": 293, "电流": 1.52},
    (50, -23.3): {"制冷量": 439, "功率": 303, "电流": 1.56},
    (50, -20): {"制冷量": 512, "功率": 324, "电流": 1.65},
    (50, -15): {"制冷量": 635, "功率": 355, "电流": 1.79},
    (50, -10): {"制冷量": 774, "功率": 386, "电流": 1.92},
    (50, -5): {"制冷量": 929, "功率": 417, "电流": 2.06},
    (50, 0): {"制冷量": 1100, "功率": 449, "电流": 2.21},
    # 第四张表格中的数据
    (55, -40): {"制冷量": 162, "功率": 202, "电流": 1.14},
    (55, -35): {"制冷量": 221, "功率": 235, "电流": 1.28},
    (55, -30): {"制冷量": 295, "功率": 269, "电流": 1.42},
    (55, -25): {"制冷量": 386, "功率": 303, "电流": 1.56},
    (55, -23.3): {"制冷量": 420, "功率": 314, "电流": 1.61},
    (55, -20): {"制冷量": 492, "功率": 336, "电流": 1.71},
    (55, -15): {"制冷量": 613, "功率": 370, "电流": 1.86},
    (55, -10): {"制冷量": 750, "功率": 404, "电流": 2.01},
    (55, -5): {"制冷量": 903, "功率": 439, "电流": 2.16},
    (55, 0): {"制冷量": 1072, "功率": 473, "电流": 2.32},
    # 第五张表格中的数据
    (60, -40): {"制冷量": 149, "功率": 203, "电流": 1.14},
    (60, -35): {"制冷量": 206, "功率": 239, "电流": 1.29},
    (60, -30): {"制冷量": 279, "功率": 276, "电流": 1.45},
    (60, -25): {"制冷量": 367, "功率": 312, "电流": 1.60},
    (60, -23.3): {"制冷量": 401, "功率": 325, "电流": 1.66},
    (60, -20): {"制冷量": 471, "功率": 349, "电流": 1.76},
    (60, -15): {"制冷量": 591, "功率": 386, "电流": 1.93},
    (60, -10): {"制冷量": 727, "功率": 423, "电流": 2.09},
    (60, -5): {"制冷量": 878, "功率": 460, "电流": 2.26},
    (60, 0): {"制冷量": 1045, "功率": 498, "电流": 2.43},
}

# CECOMAF的数据
data2 = {
    # 第一张表格中的数据
    (40, -40): {"制冷量": 217, "功率": 199, "电流": 1.13},
    (40, -35): {"制冷量": 296, "功率": 224, "电流": 1.23},
    (40, -30): {"制冷量": 389, "功率": 248, "电流": 1.33},
    (40, -25): {"制冷量": 497, "功率": 273, "电流": 1.44},
    (40, -23.3): {"制冷量": 537, "功率": 282, "电流": 1.47},
    (40, -20): {"制冷量": 619, "功率": 298, "电流": 1.54},
    (40, -15): {"制冷量": 756, "功率": 323, "电流": 1.65},
    (40, -10): {"制冷量": 907, "功率": 348, "电流": 1.76},
    (40, -5): {"制冷量": 1072, "功率": 374, "电流": 1.87},
    (40, 0): {"制冷量": 1252, "功率": 400, "电流": 1.99},
    # 第二张表格中的数据
    (45, -40): {"制冷量": 196, "功率": 200, "电流": 1.13},
    (45, -35): {"制冷量": 267, "功率": 227, "电流": 1.24},
    (45, -30): {"制冷量": 352, "功率": 255, "电流": 1.36},
    (45, -25): {"制冷量": 452, "功率": 283, "电流": 1.48},
    (45, -23.3): {"制冷量": 490, "功率": 292, "电流": 1.52},
    (45, -20): {"制冷量": 567, "功率": 311, "电流": 1.60},
    (45, -15): {"制冷量": 696, "功率": 339, "电流": 1.72},
    (45, -10): {"制冷量": 839, "功率": 367, "电流": 1.84},
    (45, -5): {"制冷量": 997, "功率": 395, "电流": 1.97},
    (45, 0): {"制冷量": 1169, "功率": 424, "电流": 2.10},
    # 第三张表格中的数据
    (50, -40): {"制冷量": 174, "功率": 201, "电流": 1.14},
    (50, -35): {"制冷量": 237, "功率": 231, "电流": 1.26},
    (50, -30): {"制冷量": 315, "功率": 262, "电流": 1.39},
    (50, -25): {"制冷量": 408, "功率": 293, "电流": 1.52},
    (50, -23.3): {"制冷量": 442, "功率": 303, "电流": 1.56},
    (50, -20): {"制冷量": 514, "功率": 324, "电流": 1.65},
    (50, -15): {"制冷量": 635, "功率": 355, "电流": 1.79},
    (50, -10): {"制冷量": 771, "功率": 386, "电流": 1.92},
    (50, -5): {"制冷量": 921, "功率": 417, "电流": 2.06},
    (50, 0): {"制冷量": 1086, "功率": 449, "电流": 2.21},
    # 第四张表格中的数据
    (55, -40): {"制冷量": 153, "功率": 202, "电流": 1.14},
    (55, -35): {"制冷量": 208, "功率": 235, "电流": 1.28},
    (55, -30): {"制冷量": 278, "功率": 269, "电流": 1.42},
    (55, -25): {"制冷量": 363, "功率": 303, "电流": 1.56},
    (55, -23.3): {"制冷量": 395, "功率": 314, "电流": 1.61},
    (55, -20): {"制冷量": 462, "功率": 336, "电流": 1.71},
    (55, -15): {"制冷量": 575, "功率": 370, "电流": 1.86},
    (55, -10): {"制冷量": 703, "功率": 404, "电流": 2.01},
    (55, -5): {"制冷量": 846, "功率": 439, "电流": 2.16},
    (55, 0): {"制冷量": 1003, "功率": 473, "电流": 2.32},
    # 第五张表格中的数据
    (60, -40): {"制冷量": 131, "功率": 203, "电流": 1.14},
    (60, -35): {"制冷量": 179, "功率": 239, "电流": 1.29},
    (60, -30): {"制冷量": 241, "功率": 276, "电流": 1.45},
    (60, -25): {"制冷量": 318, "功率": 312, "电流": 1.60},
    (60, -23.3): {"制冷量": 348, "功率": 325, "电流": 1.66},
    (60, -20): {"制冷量": 409, "功率": 349, "电流": 1.76},
    (60, -15): {"制冷量": 515, "功率": 386, "电流": 1.93},
    (60, -10): {"制冷量": 635, "功率": 423, "电流": 2.09},
    (60, -5): {"制冷量": 770, "功率": 460, "电流": 2.26},
    (60, 0): {"制冷量": 919, "功率": 498, "电流": 2.43},
}


def bilinear_interpolation(x0, y0, data):
    # 找到x0和y0的周围四个已知点
    x_vals = sorted(set([key[0] for key in data.keys()]))
    y_vals = sorted(set([key[1] for key in data.keys()]))

    # 查找x0的左右两个邻近点
    x1 = max([x for x in x_vals if x <= x0])
    x2 = min([x for x in x_vals if x >= x0])

    # 查找y0的上下两个邻近点
    y1 = max([y for y in y_vals if y <= y0])
    y2 = min([y for y in y_vals if y >= y0])

    # 如果x0和y0已经在数据点中，则直接返回该点的值
    if x1 == x2 and y1 == y2:
        return data[(x1, y1)]

    # 获取这四个点的数据
    Q11 = data[(x1, y1)]
    Q12 = data[(x1, y2)]
    Q21 = data[(x2, y1)]
    Q22 = data[(x2, y2)]

    # 插值公式：在x方向和y方向分别插值
    def linear_interp_x(p1, p2, val1, val2):
        if p2 == p1:  # 防止除以零
            return val1  # 如果p2 == p1，直接返回val1
        return val1 + (val2 - val1) * (x0 - p1) / (p2 - p1)

    def linear_interp_y(p1, p2, val1, val2):
        if p2 == p1:  # 防止除以零
            return val1  # 如果p2 == p1，直接返回val1
        return val1 + (val2 - val1) * (y0 - p1) / (p2 - p1)

    # 先对y方向进行插值
    R1 = {key: linear_interp_y(y1, y2, Q11[key], Q12[key]) for key in Q11}
    R2 = {key: linear_interp_y(y1, y2, Q21[key], Q22[key]) for key in Q21}

    # 对x方向进行插值
    result = {key: linear_interp_x(x1, x2, R1[key], R2[key]) for key in R1}

    return result

def calculate_flow_rate(refrigerant, te, tc, cooling_capacity):
    # 将温度设置为绝对温度
    Te = te + 273.15
    Tc = tc + 273.15

    # 查询制冷剂的物性参数: 压缩机入口焓和膨胀阀入口焓
    h1 = props('H', 'T', Te, 'Q', 1, refrigerant)
    h4 = props('H', 'T', Tc, 'Q', 0, refrigerant)

    # 蒸发器入口焓
    h5 = h4

    # 计算质量流量
    mass_flow_rate = cooling_capacity / (h1 - h5)
    return mass_flow_rate


# 压缩机入口和出口的管道直径
d_i = 8.1e-3
d_o = 6.5e-3
# 冷凝温度和蒸发温度
tc = 40
te = 0
# 制冷剂种类，一般为 R290 或 R600a
refrigerant = 'R290'

# 计算 1 kcal/h 等于多少 W
kcal_per_h_to_watt = (const.calorie * 1000) / const.hour

result1 = bilinear_interpolation(tc, te, data1)
# 将 kcal/h 的单位转换成 W
result1["制冷量"] *= kcal_per_h_to_watt
COP1 = result1["制冷量"] / result1["功率"]
mass_flow_rate_1 = calculate_flow_rate(refrigerant, te, tc, result1["制冷量"])

result2 = bilinear_interpolation(tc, te, data2)
COP2 = result2["制冷量"] / result2["功率"]
mass_flow_rate_2 = calculate_flow_rate(refrigerant, te, tc, result2["制冷量"])

print(f'对于第一台压缩机，制冷量1：{result1["制冷量"]:.2f} W，功率1：{result1["功率"]:.2f} W，质量流量1: {mass_flow_rate_1*3600:.2e} kg/h, COP1: {COP1:.2f}')
print(f'对于第二台压缩机，制冷量2：{result2["制冷量"]:.2f} W，功率2：{result2["功率"]:.2f} W，质量流量2: {mass_flow_rate_2*3600:.2e} kg/h, COP2: {COP2:.2f}')
